package com.wanxi;

import com.wanxi.model.StudentModel;

public class ObjectArray {
    public static void main(String[] args) {
        StudentModel[] studentModels = new StudentModel[3];
        String zhangsan = "张三,23";
        String[] zhangsans = zhangsan.split(",");
        StudentModel studentModel = new StudentModel();
        studentModel.setName(zhangsans[0]);
        studentModel.setAge(Integer.valueOf(zhangsans[1]));
        studentModels[0] = studentModel;


        String lisi = "李四,24";
        String[] lisis = lisi.split(",");
        StudentModel lisiModel = new StudentModel();
        lisiModel.setName(lisis[0]);
        lisiModel.setAge(Integer.valueOf(lisis[1]));
        studentModels[1] = lisiModel;


        String wangwu = "王五,25";
        String[] wangwus = wangwu.split(",");
        StudentModel wangwuModel = new StudentModel();
        wangwuModel.setName(wangwus[0]);
        wangwuModel.setAge(Integer.valueOf(wangwus[1]));
        studentModels[2] = wangwuModel;

        StudentModel[] aa = null;
//        if (studentModels.length == 3) {
//            aa = autoArr(studentModels);
//        }
////        将刚刚的数组里面的数据全部复制到新数组，然后将下面的model对象填充到后面
//        for (int i = 0; i < studentModels.length; i++) {
//            aa[i] = studentModels[i];
//        }
        String wangchao = "王朝,26";
        String[] wangchaos = wangchao.split(",");
        StudentModel wangchaoModel = new StudentModel();
        wangchaoModel.setName(wangchaos[0]);
        wangchaoModel.setAge(Integer.valueOf(wangchaos[1]));
//        aa[3] = wangchaoModel;
        aa = autoArr(studentModels, wangchaoModel);

        for (int i = 0; i < aa.length; i++) {
            System.out.println(aa[i]);
        }
    }

    /**
     * 自动扩容，每次扩容一个单位
     *
     * @param oldArr
     * @return
     */
    private static StudentModel[] autoArr(StudentModel[] oldArr, StudentModel model) {
        StudentModel[] newArr = new StudentModel[oldArr.length + 1];
        for (int i = 0; i < oldArr.length; i++) {
            newArr[i] = oldArr[i];
        }
        newArr[oldArr.length] = model;

        return newArr;
    }
}
